package com.yaozheng.vocationaltraining.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.LogUtils;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * Created by Administrator on 2016/6/4.
 */
@EViewGroup(R.layout.view_my_lottery)
public class MyLotteryView2 extends LinearLayout {
    @ViewById
    ImageView lotteryBgImageView, lotteryButtonImageView;

//    public static String[]
//            LOTTERY_STR = new String[]{"谢谢参与", "3积分", "100积分", "5积分",
//            "谢谢参与", "1积分", "20积分", "谢谢参与", "15积分", "1积分"};
//    public static String[]
//            LOTTERY_STR = new String[]{"3积分", "100积分", "5积分",
//            "谢谢参与", "10积分", "20积分", "谢谢参与", "15积分", "1积分", "谢谢参与",};
//    public static String[]
//            LOTTERY_STR = new String[]{"20积分", "谢谢参与", "15积分", "1积分",
//            "谢谢参与", "3积分", "100积分", "5积分", "谢谢参与", "10积分",};


    //    public static String[]
//            LOTTERY_STR = new String[]{"谢谢参与", "3积分", "100积分", "5积分",
//            "谢谢参与", "1积分", "20积分", "谢谢参与", "15积分", "1积分"};
    public static String[]
            LOTTERY_STR = new String[]{"谢谢参与", "1积分", "15积分", "谢谢参与",
            "20积分", "10积分", "谢谢参与", "5积分", "100积分", "3积分"};


    //服务器数据 跟我数据是反的 (LOTTERY_STR.length-1)-index
//    public static String[]
//            LOTTERY_STR = new String[]{"3积分", "100积分", "5积分",
//            "谢谢参与", "10积分", "20积分", "谢谢参与", "15积分", "1积分", "谢谢参与",};

    private int[] angles;

    private int winningSubscript;

    private boolean isStop;
    //开始转动时候的角度，初始值为0
    private int startDegree = 0;
    /**
     * 是否消耗
     */
    private boolean isDestroy;

    float angle;

    LotteryMonitor lotteryMonitor;

    int lastDegree;

    public MyLotteryView2(Context context) {
        super(context);
        initView();
    }

    public MyLotteryView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public int getStartDegree() {
        return startDegree;
    }

    public void setStartDegree(int startDegree) {
        this.startDegree = startDegree;
    }

    public LotteryMonitor getLotteryMonitor() {
        return lotteryMonitor;
    }

    public void setLotteryMonitor(LotteryMonitor lotteryMonitor) {
        this.lotteryMonitor = lotteryMonitor;
    }

    public void initView() {
        isDestroy = false;

        angle = (float) (360 / LOTTERY_STR.length);
        angles = new int[LOTTERY_STR.length];

//        angles[0] = (int) ((0 + angle / 2));
//        angles[1] = (int) ((angles[0] + angle / 2) + angle / 2);
//        angles[2] = (int) ((angles[1] + angle / 2) + angle / 2);
//        angles[3] = (int) ((angles[2] + angle / 2) + angle / 2);
//        angles[4] = (int) ((angles[3] + angle / 2) + angle / 2);
//        angles[5] = (int) ((angles[4] + angle / 2) + angle / 2);
//        angles[6] = (int) ((angles[5] + angle / 2) + angle / 2);
//        angles[7] = (int) ((angles[6] + angle / 2) + angle / 2);
//        angles[8] = (int) ((angles[7] + angle / 2) + angle / 2);
//        angles[9] = (int) ((angles[8] + angle / 2) + angle / 2);
        angles[0] = 18;
        angles[1] = 54;
        angles[2] = 90;
        angles[3] = 109 + 17;
        angles[4] = 145 + 17;
        angles[5] = 181 + 17;
        angles[6] = 217 + 17;
        angles[7] = 253 + 17;
        angles[8] = 289 + 17;
        angles[9] = 325 + 17;


//        for (int i = 0; i < LOTTERY_STR.length; i++) {
//            if (i == 0) {
//                angles[i] = (int) ((i == 0 ? 0 : (angles[i - 1] -angle/2) + angle/ 2) );
//            }
//        }
//        for (int i = 0; i < LOTTERY_STR.length; i++) {
//            float from = 270 - (i + 1) * angle;
//            float to = from + angle;
//            angles[i] = (int) from;
//        }
        isStop = true;
        winningSubscript = 0;
    }

    @Click(R.id.lotteryButtonImageView)
    public void lotteryButtonClick() {
        if (isStop) {
            if (lotteryMonitor != null) {
                lotteryMonitor.lotteryBegin();
            }


        }
    }


    public void beginLottery(int startDegree) {
        this.startDegree = startDegree;
        lotteryButtonImageView.setBackgroundResource(R.drawable.lottery_2);
        //初始化旋转动画，后面的四个参数是用来设置以自己的中心点为圆心转圈
        //每次转圈角度增量
        int increaseDegree = 5;
        RotateAnimation rotateAnimation = new RotateAnimation(
                0, angles[startDegree] + 360 * 5, //(angles[startDegree] + increaseDegree + 360) * 5,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        //将最后的角度赋值给startDegree作为下次转圈的初始角度
//        startDegree += increaseDegree;
        //设置动画播放时间
        rotateAnimation.setDuration(5 * 1000);
        //设置动画播放完后，停留在最后一帧画面上
        rotateAnimation.setFillAfter(true);
        //设置动画的加速行为，是先加速后减速
        rotateAnimation.setInterpolator(getContext(),
                android.R.anim.decelerate_interpolator);
        //设置动画的监听器
        rotateAnimation.setAnimationListener(al);
        //开始播放动画
        lotteryBgImageView.startAnimation(rotateAnimation);
        isStop = false;
    }

    //监听动画状态的监听器
    private Animation.AnimationListener al = new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            lotteryButtonImageView.setBackgroundResource(R.drawable.lottery_1);
            isStop = true;
            lastDegree = startDegree;
            LogUtils.println(String.valueOf(startDegree));
            LogUtils.println(String.valueOf(angles[startDegree]));
            if (lotteryMonitor != null) {
                lotteryMonitor.lotteryEnd();
            }
        }
    };

    public static interface LotteryMonitor {
        void lotteryEnd();

        void lotteryBegin();
    }
}